package com.ungao.demo.controller;

import com.ungao.demo.bean.UserInfo;
import com.ungao.demo.service.TestService;
import com.ungao.framework.annotation.Autowired;
import com.ungao.framework.web.annotation.RequestController;
import com.ungao.framework.web.annotation.RequestMapping;
import com.ungao.framework.web.annotation.RequestParam;
import com.ungao.servlet.HttpServletRequest;
import com.ungao.servlet.HttpServletResponse;

import java.util.List;

@RequestController
@RequestMapping(value = "/test")
public class TestController {

    @Autowired
    private TestService testService;

    @RequestMapping(value = "/hello")
    public UserInfo test(HttpServletRequest request, HttpServletResponse response, String name) throws Exception {
        UserInfo userInfo = new UserInfo();
        userInfo.setAge(1);
        userInfo.setName(name);
        userInfo.setId(1);
        return userInfo;
    }

    @RequestMapping(value = "/list")
    public void list(HttpServletRequest request, HttpServletResponse response, List<String> args) throws Exception {
        response.write(String.join(",", args));
    }

    @RequestMapping(value = "/all")
    public void all(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.write(testService.selectList().toString());
    }

    @RequestMapping(value = "/user")
    public void user(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        response.write(testService.selectOne(id).toString());
    }

    @RequestMapping(value = "/insert")
    public void insert(HttpServletRequest request, HttpServletResponse response, String name, int age) throws Exception {
        response.write(testService.insert(name, age) + "");
    }

    @RequestMapping(value = "/update")
    public void update(HttpServletRequest request, HttpServletResponse response, int id, String name, int age) throws Exception {
        response.write(testService.update(id, name, age) + "");
    }

    @RequestMapping(value = "/delete")
    public void delete(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        response.write(testService.delete(id) + "");
    }
}
